package com.itheima.file;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class ServerReaderThread extends Thread{
    private Socket socket;
    public ServerReaderThread(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run() {

        try {
            //1.得到一个数据输入流读取客户端发送来的数据
            DataInputStream dis=new DataInputStream(socket.getInputStream());
            //2.读取文件类型
            String suffix=dis.readUTF();
            System.out.println("服务端已经成功接受到了文件类型:"+suffix);
            //3.将文件写到服务器的文件夹里面
            OutputStream os=new FileOutputStream("C:\\Users\\dell\\IdeaProjects\\file-io-app\\bio_demo\\src\\server\\"
                    + UUID.randomUUID().toString()+suffix);

            //将文件写出去
            byte[] buffer=new byte[1024];
            int len;
            while((len=dis.read(buffer))!=-1){
                os.write(buffer,0,len);
            }
            os.close();
            System.out.println("服务端接收机文件保存成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
